package com.xmb.wcapi;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xmb.wcapi.constant.GuardConstant;
import com.xmb.wcapi.entity.Duty;
import com.xmb.wcapi.entity.DutyAgo;
import com.xmb.wcapi.entity.UniResultData;
import com.xmb.wcapi.mapper.DutyMapper;
import com.xmb.wcapi.service.*;
import com.xmb.wcapi.util.UniHttpUtil;
import com.xmb.wcapi.util.UniResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时结存 现存量
 */
@Slf4j
@Component
public class ScheduledService {

    @Resource
    IStockNowRecordService recordService;

    @Resource
    IDutyAgoService dutyAgoService;

    @Resource
    ForeignApiService foreignApiService;

    @Resource
    DutyMapper dutyMapper;

    @Value("${guard.baseUrl}")
    private String baseUrl;

    @Value("${guard.Id}")
    private String Id;

    @Value("${guard.Pwd}")
    private String Pwd;

    /**
     * 结存所有 半成品
     * 每天早上10.30点开始结转昨天的数据
     */
    @Scheduled(cron="0 10 10 * * ? ")
    public void balance(){
        recordService.balanceDataNew(DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd"));
    }

    @Scheduled(cron="0 29 14 * * ? ")
    public void recalculateNight(){

        //判断登录
        loginDuty();

        String format = DateUtil.format(new Date(), "yyyy-MM-dd");

        //执行请求
        StringBuffer buffer = new StringBuffer();
        buffer.append("?startDate=");
        buffer.append(format.replace("-","%2F"));
        buffer.append("&endDate=");
        buffer.append(format.replace("-","%2F"));
        // 执行重新结算请求
        getReload(buffer.toString());

        foreignApiService.recalculateNight(new Date());

        // 重置一下昨天的数据
        foreignApiService.recalculateNight(DateUtil.yesterday());
    }

    /**
     *  早上10:30之前需更新昨天的夜班打卡人数
     */
    @Scheduled(cron="0 0 10 * * ? ")
    public void recalculateDay(){

        //判断登录
        loginDuty();

        String format = DateUtil.format(new Date(), "yyyy-MM-dd");

        //执行请求
        StringBuffer buffer = new StringBuffer();
        buffer.append("?startDate=");
        buffer.append(format.replace("-","%2F"));
        buffer.append("&endDate=");
        buffer.append(format.replace("-","%2F"));
        // 执行重新结算请求
        getReload(buffer.toString());

        foreignApiService.recalculateDay(new Date());

        // 重置一下昨天的数据
        foreignApiService.recalculateDay(DateUtil.yesterday());

    }

    private void getReload(String whereBuffer){
        String queryPersonUrl = baseUrl + GuardConstant.URL_RECOUNT_CODE + whereBuffer;
        try {
            UniHttpUtil.sendHttpGetJson(queryPersonUrl, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void loginDuty(){
        try {
            UniHttpUtil.getCookieStore(baseUrl+GuardConstant.LOGIN_URL,Id, DigestUtils.md5Hex(Pwd));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
